package net.kts.attraction;


import net.kts.attraction.search.model.Address;
import net.kts.attraction.search.overlay.StartDestMyItemizedOverlay;
import net.kts.help.HelpLayout;
import net.kts.help.HelpPreferences;
import net.kts.help.HelpService;
import net.kts.location.MyLocationManager;
import net.kts.main.R;
import net.kts.util.url.mylocation.MyLocationDrawable;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class AttractionMap extends MapActivity {

	private MapView mapView = null;
//	private LocationManager locationManager = null;
//	private MyLocationOverlay myLocationOverlay = null;
	private LocationListener locationListener = null;
	private String TAG = "kts";
	private Location location = null;
	private Address address = null;
	
	private ProgressDialog progressDialog = null;
	
	private MyLocationDrawable myLocationDrawable = null;
	
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.attraction_search_dest_map);
		
		this.mapView = (MapView)findViewById(R.id.attractionMap);
		
		this.progressDialog =  new ProgressDialog(this);//ProgressDialog.show(this, "Search Target Position", "Searching...");
		this.progressDialog.setMessage("Search Target Position..");
		this.progressDialog.show();
		
		Drawable drawable = getResources().getDrawable(R.drawable.story_flag_my);
		this.myLocationDrawable = new MyLocationDrawable(drawable);
		
		getIntentInit();
		
//		final Drawable d = getResources().getDrawable(R.drawable.marker_start);
//        Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
		
		double latltude = Double.valueOf(this.address.getLatitude());
		double longitude = Double.valueOf(this.address.getLongitude());
		
		GeoPoint geoPoint = new GeoPoint((int)(latltude * 1000000), (int)(longitude * 1000000));
		
//		this.locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		//this.mapView.s
//		this.myLocationOverlay = new MyLocationOverlay(this, this.mapView);
		
		
		
		
		
		//this.mapView.getController().setCenter(geoPoint);
		this.mapView.getController().setZoom(12);
		this.mapView.setBuiltInZoomControls(true);
        //this.mapView.setClickable(true);
        
        
        // ��ġ ���
//        AttractionLocations locations = new AttractionLocations(getResources().getDrawable(R.drawable.marker_dest));
//        locations.addLocation(geoPoint, "title", "snippet");
		Location currLocation = MyLocationManager.giveMeMyCurrentLocation();
		Location destLocation = new Location("dest");
		destLocation.setLatitude(latltude);
		destLocation.setLongitude(longitude);
		
		double distance = currLocation.distanceTo(destLocation);
		
		StartDestMyItemizedOverlay startDestMyItemizedOverlay = new StartDestMyItemizedOverlay(getResources().getDrawable(R.drawable.marker_dest), this.mapView);
		OverlayItem overlayItem = new OverlayItem(geoPoint, this.address.getAddressFullName(), this.address.getTransText() + "||" + (int)distance);
		startDestMyItemizedOverlay.addOverlay(overlayItem);
        //locations.addLocation(new GeoPoint((int)(37.5392864243*1000000), (int)(127.1227038889*1000000)), "title", "snippet");
       
        //locations.addLocation(new GeoPoint((int)(37.5437887744*1000000), (int)(127.1269736306*1000000)), "title", "snippet");
        
        this.mapView.getOverlays().add(startDestMyItemizedOverlay);
		
		// ����ġ ����
		
//		myLocationOverlay = new MyLocationOverlay(this, this.mapView);
//        this.mapView.getOverlays().add(myLocationOverlay);
//        
//        this.myLocationOverlay.enableCompass();
//        this.myLocationOverlay.enableMyLocation();
        
        
        //String context = Context.LOCATION_SERVICE;
        //this.locationManager = (LocationManager)getSystemService(context);
        
//        Criteria criteria = new Criteria();
//        criteria.setAccuracy(Criteria.ACCURACY_FINE);
//        criteria.setAltitudeRequired(false);
//        criteria.setBearingRequired(false);
//        criteria.setCostAllowed(true);
//        criteria.setPowerRequirement(Criteria.POWER_LOW);
        
//        String provider = locationManager.getBestProvider(criteria, true);
//        
//        Location location = locationManager.getLastKnownLocation(provider);
//        updateWidthNewLocation(location);
//        
//        locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

        
     
        if(HelpPreferences.isShowing())
	        new HelpService(this)
	        		.add(new HelpLayout(R.drawable.help_search_dest));
		
	}
	
	private void locationListnerInit() {
		this.locationListener = new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				updateWidthNewLocation(null);
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				updateWidthNewLocation(location);
			}
		};
	}
	
	private void getIntentInit() {
		 Intent intent = getIntent();
		 Bundle bundle = intent.getExtras();
		 this.address = (Address)bundle.get("address");
		
	}
	
	public void updateWidthNewLocation(Location location){
    	
    	if(location !=null){    		
    		Double geoLat = location.getLatitude()*1000000;
    		Double geoLng = location.getLongitude()*1000000;
    		GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
    		
    		this.myLocationDrawable.updateOverlay(new OverlayItem(point, "", ""));
    		this.mapView.getOverlays().add(myLocationDrawable);
    		
    		this.mapView.getController().animateTo(point);    		
    	}
    }
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		if(this.locationListener == null) {
			this.locationListnerInit();
			MyLocationManager.addLocationListener(this.locationListener);
		}
		
		Location location = MyLocationManager.giveMeMyCurrentLocation();
		showLocation(location);
		
		GeoPoint point = new GeoPoint((int)(location.getLatitude() * 1000000), (int)(location.getLongitude()* 1000000 ));
		
		this.myLocationDrawable.updateOverlay(new OverlayItem(point, "", ""));
		this.mapView.getOverlays().add(myLocationDrawable);
		
		this.mapView.getController().animateTo(point);    
		
//		Location lastLoc = this.locationManager.getLastKnownLocation(this.locationManager.GPS_PROVIDER);
//		
//		if(lastLoc != null) {
//			showLocation(lastLoc);
//		}
//		
//		this.locationManager.requestLocationUpdates(this.locationManager.GPS_PROVIDER, 0, 0, this.locationListener);
//		
//		this.myLocationOverlay.enableMyLocation();
//		
//		this.myLocationOverlay.runOnFirstFix(new Runnable() {
//			
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				mapView.getController().setCenter(myLocationOverlay.getMyLocation());
//			}
//		});
		
	}

	private void showLocation(Location location) {
    	if(location != null) {
    		double lat = location.getLatitude();
    		double lng = location.getLongitude();
    		
    		GeoPoint myLocation = new GeoPoint((int)lat*1000000, (int)lng*1000000);
    		
//    		Toast.makeText(getBaseContext(), "New location latitude ["  +
//    		lat + "] longitude [" + lng + "]", Toast.LENGTH_SHORT).show();
    		
    		this.mapView.getController().animateTo(myLocation);
    		
//    		MapView.LayoutParams mapMarkerParams = new MapView.LayoutParams(
//    				LayoutParams.WRAP_CONTENT,
//    				LayoutParams.WRAP_CONTENT, myLocation,
//    				MapView.LayoutParams.TOP_LEFT);
//    		ImageView mapMarker = new ImageView(
//    				getApplicationContext());
//    		mapMarker.setImageResource(R.drawable.mapmarker);
//    		this.mapView.addView(mapMarker, mapMarkerParams);
    	}
    	
    	this.progressDialog.dismiss();
    }


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
//		this.locationManager.removeUpdates(this.locationListener);
//		this.myLocationOverlay.disableMyLocation();
	}

	

	
}
